package com.zhdl.modules.demo.dglProcess;

import com.project.common.util.DelayUtil;
import com.zhdl.common.ytools.callback.CommonCallback;
import com.zhdl.modules.demo.DCHTProcess.CameraRunDo;
import com.zhdl.modules.demo.DCHTProcess.CylinderControlDo;
import com.zhdl.modules.demo.DCHTProcess.MotorABSRunDo;
import com.zhdl.modules.demo.DCHTProcess.RobotRunDo;
import com.zhdl.modules.demo.dglProcess.Robot.RobotChildProcess;
import com.zhdl.modules.demo.dglProcess.Robot.WorkPlace1RobotInterface;
import com.zhdl.modules.demo.dglProcess.camera.CameraResult;
import com.zhdl.modules.demo.dglProcess.camera.DGLCameraService;
import com.zhdl.modules.process.common.ProcessDo;
import com.zhdl.modules.process.common.ProcessTimeout;
import com.zhdl.modules.process.common.ProcessVerify;
import com.zhdl.modules.process.dto.ProcessBean;
import com.zhdl.modules.process.service.MainProcessValueProvider;
import com.zhdl.modules.process.service.ProjectConfigProcessService;
import com.zhdl.modules.process.task.CustomizeTask;
import com.zhdl.modules.process.task.DynamicTask;
import com.zhdl.modules.process.verify.DelayTimeVerify;
import com.zhdl.modules.process.verify.PlCBitValueVerify;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DGLProcessInitService {

    @Resource
    WorkPlace1RobotInterface workPlace1RobotInterface;
    @Resource
    ProjectConfigProcessService projectConfigProcessService;
    @Resource
    MainProcessValueProvider mainProcessValueProvider;
    @Resource
    DGLCameraService dglCameraService;
    @Resource
    DynamicTask dynamicTask;
    /**
     * 1 代表成功 2代表失败
     */
    private int cameraResult = 0;
    public List<ProcessBean> initMainList(){
        List<ProcessBean> processBeanList = new ArrayList<>();
        processBeanList.add(
                new ProcessBean("主流程整机控制只做状态管理不参与真实流程控制", 1, 0)
                                .addDoSomeThing(() -> {
                                })
                                .addProcessVerify(() -> false));
        return processBeanList;
    }
    public List<ProcessBean> initWorkPace1(WorkPlace1Process workPlace1Process){
        workPlace1RobotInterface.reSetAll();
        mainProcessValueProvider.reSetAll();
        List<ProcessBean> processBeanList = new ArrayList<>();
        processBeanList.add(
                new ProcessBean("机器人移动至导电棒物料上方")
                        .addDoSomeThing(new RobotRunDo(workPlace1RobotInterface,RobotChildProcess.PICK_UP))
                        .addProcessVerify(new PlCBitValueVerify(workPlace1RobotInterface, workPlace1RobotInterface.ROBOT_DONE)));

        processBeanList.add(
                new ProcessBean("通知相机拍照识别")
                        .addDoSomeThing(new CameraRunDo(dglCameraService, 1, 1, cameraResult -> {
                            dglCameraService.getValue(DGLCameraService.TAKE_PHOTO_DONE).setValue("1");
                            dglCameraService.getValue(DGLCameraService.RESULT_OK).setValue(cameraResult.isResultOK()?"1":"0");
                            if (cameraResult.isResultOK()) {
                                dglCameraService.REGIONLOCATION = cameraResult.getLocation();
                                log.info("相机识别结果{}",dglCameraService.REGIONLOCATION);
                            }
                        }))
                        .addAfterProcess(lastProcess -> {
                            if (dglCameraService.getValue(DGLCameraService.RESULT_OK).getValue().equals("1")){
                                workPlace1Process.startProcess(lastProcess+1);
                            }else{
                                workPlace1Process.startProcess(lastProcess);
                            }
                            dglCameraService.reSetAll();
                        })
                        .addProcessVerify(new PlCBitValueVerify(dglCameraService,DGLCameraService.TAKE_PHOTO_DONE)));


        processBeanList.add(
                new ProcessBean("机器人移动至抓料点上方 有上方至抓料点")
                        .addDoSomeThing(new RobotRunDo(workPlace1RobotInterface))
                        .addProcessVerify(new PlCBitValueVerify(workPlace1RobotInterface, workPlace1RobotInterface.ROBOT_DONE)));


        processBeanList.add(
                new ProcessBean("机器人夹具夹紧",2000,true)
                        .addDoSomeThing(
                            new CylinderControlDo(DGLStaticDeviceName.ROBOT_JJ,true)
                        )
                        .addProcessVerify(new PlCBitValueVerify(projectConfigProcessService, ProjectConfigProcessService.COMMON_TEST_BIT)));

        processBeanList.add(
                new ProcessBean("机器人到达下料点",6000)
                        .addDoSomeThing(new RobotRunDo(workPlace1RobotInterface))
                        .addProcessVerify(new PlCBitValueVerify(workPlace1RobotInterface, workPlace1RobotInterface.ROBOT_DONE)));


        processBeanList.add(
                new ProcessBean("装配位气缸夹紧",1000)
                        .addDoSomeThing(
                            new CylinderControlDo(DGLStaticDeviceName.ASSEMBLE_JJ,true)
                       )
                        .addProcessVerify(new PlCBitValueVerify(projectConfigProcessService, ProjectConfigProcessService.COMMON_TEST_BIT)));

        processBeanList.add(
                new ProcessBean("机器人夹具张开",1000)
                        .addDoSomeThing(
                            new CylinderControlDo(DGLStaticDeviceName.ROBOT_JJ,false)
                        )
                        .addProcessVerify(new PlCBitValueVerify(projectConfigProcessService, ProjectConfigProcessService.COMMON_TEST_BIT)));

        processBeanList.add(
                new ProcessBean("机器人执行完放料流程回原点",3000)
                        .addDoSomeThing(new RobotRunDo(workPlace1RobotInterface))
                        .addProcessVerify(new PlCBitValueVerify(workPlace1RobotInterface, workPlace1RobotInterface.ROBOT_DONE)));

        processBeanList.add(
                new ProcessBean("法奥机器人到达套缸缺料位")
                        .addDoSomeThing(() -> {
                            workPlace1RobotInterface.robotNextStep();
                            workPlace1Process.startGlueProcess();
                        })
                        .addProcessVerify(new PlCBitValueVerify(projectConfigProcessService, ProjectConfigProcessService.COMMON_TEST_BIT)));

        processBeanList.add(
                 new ProcessBean("法奥机器人夹具加紧")
                .addDoSomeThing(
                    new CylinderControlDo(DGLStaticDeviceName.法奥机器人夹具,true)
                )
                .addProcessVerify(new PlCBitValueVerify(projectConfigProcessService, ProjectConfigProcessService.COMMON_TEST_BIT)));
                    //省略掉法奥机器人剩余动作
        processBeanList.add(
                new ProcessBean("等待涂胶流程完成")
                        .addDoSomeThing(() -> {
                            log.info("等待子流程完成信号-------------------------------------------------------------------------------");
                        })
                        .addProcessVerify(new PlCBitValueVerify(mainProcessValueProvider, MainProcessValueProvider.CHILD_PROCESS_DONE)));

        processBeanList.add(
                new ProcessBean("法奥机器人到达放料点上方")
                        .addDoSomeThing(() -> {
                            workPlace1RobotInterface.robotNextStep();
                        })
                        .addProcessVerify(new PlCBitValueVerify(projectConfigProcessService, ProjectConfigProcessService.COMMON_TEST_BIT)));


        return processBeanList;
    }

    /**
     * 工位1初始化流程
     */
    public void startWorkPace1InitLocation(CommonCallback<String> initComplete){
        List<ProcessBean> processBeanList = new ArrayList<>();
        processBeanList.add(
                new ProcessBean("工位一初始化流程1", 1, 0)
                        .addDoSomeThing(() -> {
                            workPlace1RobotInterface.startChildProcess(RobotChildProcess.INIT);
                        })
                        .addAfterProcess(lastProcess -> {
                            dynamicTask.startTask(new CustomizeTask(processBeanList.get(1)),100);
                        })
                        .addProcessVerify(new DelayTimeVerify(3000)));
        processBeanList.add(
                new ProcessBean("工位一初始化流程2", 1, 0)
                        .addDoSomeThing(() -> {
                            workPlace1RobotInterface.robotNextStep();
                        })
                        .addAfterProcess(lastProcess -> initComplete.statusCallback("ok"))
                        .addProcessVerify(new DelayTimeVerify(3000)));
        dynamicTask.startTask(new CustomizeTask(processBeanList.get(0)),100);
    }

    public List<ProcessBean> getGlueProcessList(){
        List<ProcessBean> processBeanList = new ArrayList<>();
        processBeanList.add(
                new ProcessBean("横移电机移动至涂胶位")
                        .addDoSomeThing(() -> {
                            new MotorABSRunDo(DGLStaticDeviceName.MOTOR_1,1);
                        })
                        .addProcessVerify(new PlCBitValueVerify(projectConfigProcessService,  ProjectConfigProcessService.COMMON_TEST_BIT)));
        processBeanList.add(
                new ProcessBean("横移电机升降气缸下降")
                        .addDoSomeThing(
                           new CylinderControlDo(DGLStaticDeviceName.横移升降气缸,true)
                        )
                        .addProcessVerify(new PlCBitValueVerify(projectConfigProcessService, ProjectConfigProcessService.COMMON_TEST_BIT)));

        new ProcessBean("点胶机打开")
                .addDoSomeThing(() -> {
                    new CylinderControlDo(DGLStaticDeviceName.点胶机,true).doSomeThing();
                    DelayUtil.delayedExecution(1000, () -> new CylinderControlDo(DGLStaticDeviceName.点胶机,false));
                })
                .addProcessVerify(new PlCBitValueVerify(projectConfigProcessService, ProjectConfigProcessService.COMMON_TEST_BIT));

        processBeanList.add(
                new ProcessBean("横移电机升降气缸上升")
                        .addDoSomeThing(() -> {
                            new CylinderControlDo(DGLStaticDeviceName.横移升降气缸,false).doSomeThing();
                        })
                        .addProcessVerify(new PlCBitValueVerify(projectConfigProcessService, ProjectConfigProcessService.COMMON_TEST_BIT)));

        addProcessToList(processBeanList,"横移电机移动至等待位", new MotorABSRunDo(DGLStaticDeviceName.MOTOR_1,0));
        //后面省略
        return processBeanList;
    }

    private void addProcessToList(List<ProcessBean> processBeanList, String desc, ProcessDo processDo, ProcessVerify processVerify){
        processBeanList.add(new ProcessBean(desc)
                .addDoSomeThing(processDo)
                .addProcessVerify(processVerify));
    }
    private void addProcessToList(List<ProcessBean> processBeanList, String desc, ProcessDo processDo){
        processBeanList.add(new ProcessBean(desc)
                .addDoSomeThing(processDo)
                .addProcessVerify(new PlCBitValueVerify(projectConfigProcessService, ProjectConfigProcessService.COMMON_TEST_BIT)));
    }

    public List<ProcessBean> initWorkPace2(){
        List<ProcessBean> processBeanList = new ArrayList<>();
        addProcessToList(processBeanList,"工位二模拟流程1",new CylinderControlDo("1111",true));
        addProcessToList(processBeanList,"工位二模拟流程2",new CylinderControlDo("2222",true));
        addProcessToList(processBeanList,"工位二模拟流程3",new CylinderControlDo("3333",true));
        addProcessToList(processBeanList,"工位二模拟流程4",new CylinderControlDo("4444",true));
        return  processBeanList;
    }
    public List<ProcessBean> initWorkPace3(){
        List<ProcessBean> processBeanList = new ArrayList<>();
        addProcessToList(processBeanList, "等待缺料信号", () -> {

        });
        addProcessToList(processBeanList,"工位三模拟流程1",new CylinderControlDo("1111",true));
        addProcessToList(processBeanList,"工位三模拟流程2",new CylinderControlDo("2222",true));
        addProcessToList(processBeanList,"工位三模拟流程3",new CylinderControlDo("3333",true));
        addProcessToList(processBeanList,"工位三模拟流程4",new CylinderControlDo("4444",true));
        return  processBeanList;
    }

}
